<template>
  <div class="depart-container">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm" size="small" class="search-form">
        <el-form-item style="margin-bottom:0;margin-right:8px;">
          <el-input v-model="searchForm.query" placeholder="系名关键字" style="width:180px;" />
        </el-form-item>
        <el-form-item style="margin-bottom:0;margin-right:8px;">
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
        </el-form-item>
        <el-form-item style="margin-bottom:0;margin-right:8px;">
          <el-button :icon="Refresh" @click="handleReset">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-bottom:0;">
          <el-button type="success" :icon="Plus" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table :data="tableData" border style="width: 100%" v-loading="loading" size="small">
        <el-table-column type="index" label="序号" width="60" align="center" :index="indexMethod" fixed="left" />
        <el-table-column prop="department_name" label="系名称" :min-width="220" show-overflow-tooltip />
        <el-table-column prop="major_count" label="专业数量" width="120" align="center" />
        <el-table-column prop="teacher_count" label="教师数量" width="120" align="center" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <div style="display: flex">
              <el-button size="small" :icon="Edit" @click="handleEdit(row)">编辑</el-button>
              <el-button size="small" type="danger" :icon="Delete" @click="handleDelete(row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.pageNo"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        size="small"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  <!-- 添加/编辑对话框 -->
  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%" class="form-dialog" modal-class="light-modal-mask">
      <el-form :model="dialogForm" label-width="80px" size="small">
        <el-form-item label="系名称">
          <el-input v-model="dialogForm.department_name"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="dialogVisible = false">取消</el-button>
          <el-button size="small" type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 自定义删除确认弹窗 -->
    <BaseConfirmDialog
      :visible="confirmVisible"
      title="提示"
      :message="confirmMessage"
      confirm-text="确定"
      cancel-text="取消"
      @confirm="confirmDelete"
      @cancel="confirmVisible=false"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Search, Refresh, Plus, Edit, Delete } from '@element-plus/icons-vue'
import {
  getFacultiesAPI,
  addFacultyAPI,
  updateFacultyAPI,
  deleteFacultyAPI
} from '@/api/admin/faculties.js'
import BaseConfirmDialog from '@/components/BaseConfirmDialog.vue'

// 搜索表单
const searchForm = reactive({
  query: ''
})

// 表格数据
const tableData = ref([])

// 加载态
const loading = ref(false)

// 分页
const pagination = reactive({
  pageNo: 1,
  pageSize: 10,
  total: 0
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('')
const dialogForm = reactive({
  department_id: '',
  department_name: ''
})
const isEdit = ref(false)

// 删除确认弹窗
const confirmVisible = ref(false)
const confirmMessage = ref('')
let pendingDeleteId = ''

// 获取系数据
const fetchFaculties = async () => {
  loading.value = true
  const params = {
    pageNo: pagination.pageNo,
    pageSize: pagination.pageSize,
    query: searchForm.query
  }
  try {
    const res = await getFacultiesAPI(params)
    tableData.value = res.list || []
    pagination.total = res.total || 0
  } finally {
    loading.value = false
  }
}

onMounted(fetchFaculties)

// 序号（跨页连续）
const indexMethod = (index) => {
  return (pagination.pageNo - 1) * pagination.pageSize + index + 1
}

// 搜索
const handleSearch = () => {
  pagination.pageNo = 1
  fetchFaculties()
}

// 重置
const handleReset = () => {
  searchForm.query = ''
  pagination.pageNo = 1
  pagination.pageSize = 10
  fetchFaculties()
}

// 添加
const handleAdd = () => {
  isEdit.value = false
  dialogTitle.value = '添加系'
  dialogForm.department_id = ''
  dialogForm.department_name = ''
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  isEdit.value = true
  dialogTitle.value = '编辑系'
  dialogForm.department_id = row.department_id
  dialogForm.department_name = row.department_name
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  pendingDeleteId = row.department_id
  confirmMessage.value = `确定要删除系 "${row.department_name}" 吗？`
  confirmVisible.value = true
}
const confirmDelete = async () => {
  await deleteFacultyAPI({ department_id: pendingDeleteId })
  confirmVisible.value = false
  fetchFaculties()
}

// 提交表单
const handleSubmit = async () => {
  if (isEdit.value) {
    // 编辑
    await updateFacultyAPI(dialogForm)
  } else {
    // 添加
    await addFacultyAPI({ department_name: dialogForm.department_name })
  }
  dialogVisible.value = false
  fetchFaculties()
}

// 分页大小改变
const handleSizeChange = (val) => {
  pagination.pageSize = val
  fetchFaculties()
}

// 当前页改变
const handleCurrentChange = (val) => {
  pagination.pageNo = val
  fetchFaculties()
}
</script>

<style lang="scss" scoped>
.depart-container {
  padding: 8px 20px 20px 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.search-bar {
  display: flex;
  align-items: center;
  padding: 0;
  margin: 0 0 18px 0;
  background: none;
  border: none;
  box-shadow: none;
}
.search-form {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 0;
  margin: 0;
}
.search-form .el-form-item {
  margin-bottom: 0;
  margin-right: 8px;
}
.search-form .el-input__wrapper,
.search-form .el-button {
  height: 26px !important;
  min-height: 26px !important;
  font-size: 13px;
}
.table-card {
  margin-top: 0;
}
.el-table {
  margin-top: 0;
}
.el-pagination {
  margin-top: 6px;
  display: flex;
  justify-content: flex-start;
}
.el-pagination .el-pager li {
  border-radius: 4px;
  border: 1px solid #e4e7ed;
  background: #fff;
  color: #606266;
  min-width: 32px;
  height: 32px;
  line-height: 32px;
  margin: 0 2px;
  font-size: 15px;
  cursor: pointer;
}
.el-pagination .el-pager li.active {
  background: #409eff;
  color: #fff;
  border-color: #409eff;
}
.table-card {
  flex: 1 1 0%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding-top: 6px;
}
.table-card :deep(.el-card__body) {
  flex: 1 1 0%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.table-card :deep(.el-table) {
  flex: 1 1 auto;
  overflow: auto;
}
.el-pagination {
  margin-top: 8px;
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
}
</style>
